package top.laoshuzi.scad.mosinnagant.utils

import eu.printingin3d.javascad.coords.Coords3d
import eu.printingin3d.javascad.models.Abstract3dModel
import top.laoshuzi.scad.mosinnagant.model.ExtPolyhedron
import top.laoshuzi.scad.mosinnagant.utils.ex.*

/**
 * Created by mouse on 2019/7/19.
 */

object MosinNagant {

    val LEHTH_MULTIPLE: Double = 1.0

    fun l(l: Double) = LEHTH_MULTIPLE * l


    //撞针
    fun firingPin(): Abstract3dModel {
        return ScadUtils.anchor()
            .addBottom(
                Cy(1.0, 0.5, 0.1)
            )
            .addBottom(
                Cy(4.0, 0.5)
            )
            .addBottom(
                Cy(1.0, 1.0, 0.5)
            )
            .addBottom(
                Cy(5.0, 1.0)
            )
            .addBottom(
                Cy(2.0, 0.75)
            )
    }

    //拉栓
    fun bolt(): Abstract3dModel {
        return ScadUtils.anchor()
            .addBottom(
                Cy(2.0, 0.75)
            )
            .addBottom(
                Cy(1.0, 0.75, 1.5)
            )
            .addBottom(
                Cy(1.0, 0.75)
            )
            .addBottom(
                Cy(1.0, 1.5, 0.75)
            )
            .addBottom(
                Cy(3.0, 2.5)
                    //
                    .addFront(
                        Cu(2.0, 2.0, 2.0),
                        out = false
                    )
                    .addFront(
                        CuS4(1.5, 2.0, 1.5)
                            .Rx(90.0)
                    ).addFront(
                        Cu(1.5, 2.0, 1.5)
                    )
                    .addFront(
                        Sp(1.5)
                            .addBack(
                                Cu(1.5, 1.5, 1.5),
                                out = false
                            )
                    )
                    //
                    .addRight(
                        Cu(1.5, 0.0, 1.5)
                            .addFront(
                                Cu(1.5, 2.5, 1.5)
                            ),
                        out = false
                    )

            )
            .addBottom(
                Cy(0.5, 1.0, 2.5)
            )
    }


    //撞针管
    fun firingPinTube(): Abstract3dModel {
        return ScadUtils.anchor()
            .addBottom(
                Cu(5.0, 5.0, 24.0)
            )
            .run {
                intersection(
                    ScadUtils.anchor()
                        .addBottom(
                            Cy(24.0, 2.5)
                        )
                )
            }
    }

    //撞针管内槽
    fun firingPinTubeInGroove(): Abstract3dModel {
        return ScadUtils.anchor()
            .addBottom(
                Cy(5.0, 0.5)
            )
            .addBottom(
                Cy(1.0, 1.0, 0.5)
            )
            .addBottom(
                Cy(5.0 + 10.0, 1.0)
            )
            .addBottom(
                Cy(1.0, 1.0, 1.5)
            )
            .addBottom(
                Cy(1.0, 1.0)
            )
            .addBottom(
                Cy(1.0, 1.5, 1.0)
            )
    }

    //撞针管下槽
    fun firingPinTubeBottomGroove(): Abstract3dModel {
        return ScadUtils.anchor()
            .addBottom(
                CuS4(1.0, 1.0, 24.0, 2.0, 24.0)
                    .Rx(90.0),
                center = true
            )
    }

    //撞针管下半部分
    fun firingPinTubeBottom(): Abstract3dModel {

        val firingPinTube = firingPinTube()

        val firingPinTubeInGroove = firingPinTubeInGroove()

        val firingPinTubeBottomGroove = firingPinTubeBottomGroove()
            .Ty(2.0)

        return ScadUtils.anchor()
            .addBack(
                Cu(5.0, 2.5, 0.0)
                    .addBottom(
                        Cu(0.0, 0.0, 1.0)
                    )
                    .addBottom(
                        Cu(5.0, 2.5, 24.0)
                    )
            ).run {
                intersection(
                    difference(firingPinTubeInGroove, firingPinTubeBottomGroove),
                    firingPinTube
                )
            }
    }

    //撞针管上半部分
    fun firingPinTubeTop(): Abstract3dModel {

        val firingPinTubeInGroove = firingPinTubeInGroove()

        val firingPinTube = firingPinTube()

        return ScadUtils.anchor()
            .addFront(
                Cu(5.0, 2.5, 0.0)
                    .addBottom(
                        Cu(0.0, 0.0, 1.0)
                    )
                    .addBottom(
                        Cu(5.0, 2.5, 24.0)
                    )
            ).run {
                intersection(
                    difference(firingPinTubeInGroove),
                    firingPinTube
                )
            }
    }

    //上扳机
    fun triggerTop(): Abstract3dModel {

        val triggerAxis =
            Cy(0.8, 0.5)

        val triggerArm1 =
            Cu(1.5, 1.5, 0.8)
                .difference(
                    CuS3(0.8, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0)
                        .Tx(-0.25)
                        .Ty(-0.25),
                    CuS3(0.8, -1.5, -1.0, 0.0, -1.0, -1.0, 0.0)
                        .Ty(0.25)
                )

        val triggerArm2 =
            union(
                Cu(1.0, 1.5, 0.8)
                    .difference(
                        CuS3(0.8, -0.5, 1.5, 0.0, -0.5, 1.5, 0.0)
                            .Tx(0.25)
                    ),
                CuS3(0.8, -0.25, 0.5, 0.0, -0.25, 0.5, 0.0)
                    .Tx(-0.625)
                    .Ty(-0.5),
                Cy(0.5, 0.2)
                    .Ry(90.0)
                    .Ty(-0.5)
            )

        return union(
            triggerAxis,
            triggerArm1
                .Tx(-0.75)
                .Ty(0.75),
            triggerArm2
                .Ty(-0.75)
        ).difference(
            Cy(0.8, 0.25)
        )
    }

    //下扳机
    fun triggerBottom(): Abstract3dModel {

        val triggerAxis =
            Cy(0.8, 0.5)

        val triggerArm1 =
            Cy(0.8, 0.5)
                .Sy(2.25)
                .difference(
                    Cu(1.0, 2.25, 0.8)
                        .Tx(0.5),
                    Cu(1.0, 2.25, 0.8)
                        .Ty(-1.125)
                )

        val triggerArm2 =
            union(
                Cu(1.0, 1.5, 0.8)
                    .difference(
                        CuS3(0.8, 0.75, 1.5, 0.0, 0.75, 1.5, 0.0)
                            .Tx(-0.125),
                        CuS3(0.8, -0.5, -1.5, 0.0, -0.5, -1.5, 0.0)
                            .Tx(0.25)
                    )
            )

        return union(
            triggerAxis,
            triggerArm1,
            triggerArm2
                .Ty(-0.75)
        ).difference(
            Cy(0.8, 0.25)
        )
    }
}
